package com.changyue.activity.me;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.changyue.activity.basic.BasicActivity;
import com.changyue.bleHelper.ScanActivity;
import com.changyue.bleHelper.BLEDeviceConfig;
import com.changyue.temp.BuildConfig;
import com.changyue.temp.databinding.ActivitySettingBinding;
import com.changyue.util.AppUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class SettingActivity extends BasicActivity {

    private ActivitySettingBinding binding;


    public static void start(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivitySettingBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
        loadData();

        EventBus.getDefault().register(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isNeedUpdate) {
            isNeedUpdate = false;
            loadData();
        }
    }

    @Subscribe
    public void onEventMainThread(String event) {
        if (event != null && event.contains("Setting")) {
            isNeedUpdate = true;
        }
    }

    public void  initView() {
        binding.nameLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingNameActivity.start(SettingActivity.this);
            }
        });

        binding.bleLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScanActivity.start(SettingActivity.this);
            }
        });

        binding.periodLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SettingPeriodActivity.start(SettingActivity.this);
            }
        });


        binding.calibrateLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CalibrateActivity.start(SettingActivity.this);
            }
        });

        binding.aboutLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AboutActivity.start(SettingActivity.this);
            }
        });

        String versionInfo = "V " + AppUtils.getVersionName(this) + "(" + AppUtils.versionCode(this) + ")" + (BuildConfig.DEBUG ? "测试环境" : "");
        binding.version.setText(versionInfo);
    }

    public void loadData() {
        binding.name.setText(BLEDeviceConfig.getInstance().getTypeName());
        binding.period.setText(BLEDeviceConfig.getInstance().getPeriodUnit());
    }
}